from django.contrib import admin

from . import CommodityConfig
# Register your models here.
from .models import *

admin.site.site_title = '母婴后台系统'
admin.site.site_header = "母婴电商后台管理系统"
admin.site.index_title = "母婴平台管理"

@admin.register(Types)
class TypesAdmin(admin.ModelAdmin):
    list_display = [x for x in list(Types._meta._forward_fields_map.keys())]
    search_fields = ['firsts', 'seconds']
    list_filter = ['firsts']

"""@admin.register(CommodityInfos)
class CommodityInfosAdmin(admin.ModelAdmin):
    list_display =  [x for x in list(CommodityInfos._meta._forward_fields_map.keys())]
    search_fields = ['name']
    date_hierarchy = 'create'
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'types':
            db_field.choices = [(x['seconds'], x['seconds']) for x in Types.objects.values('seconds') ]
        return super().formfield_for_dbfield(db_field, **kwargs)
"""
"""
@admin.register(CommodityInfos)
class CommodityInfosAdmin(admin.ModelAdmin):

    fieldsets = (
        ('商品信息',{
            'fields': ('name', 'sezes', 'types', 'price', 'discount')
        }),
        ('收藏数量',{
            'classes': ('collapse',),
            'fields': ('likes',),
        }),
    )
    ordering = ['id']
    sortable_by = ('price', 'discount')
    list_display = ['id', 'name','sezes','types', 'price', 'discount']
    list_filter = ['types']
    list_per_page = 100
    list_max_show_all = 200
    list_editable = ['name']
    search_fields = ['name', 'types']
    save_as = True
    actions_on_top = False
    actions_on_bottom = True
    """

@admin.register(CommodityInfos)
class CommodityInfosAdmin(admin.ModelAdmin):
    list_display =  [x for x in list(CommodityInfos._meta._forward_fields_map.keys())]
    # 按照name字段进行搜索
    search_fields = ['name']
    # 设置按照哪个日期字段进行层级的导航
    date_hierarchy = 'create'
    # 重新定义函数formfield_for_dbfield，用来自定义模型字段在表单中的显示
    def formfield_for_dbfield(self, db_field, **kwargs):
        # 我们这里主要针对types字段
        if db_field.name == 'types':
            # 动态设置types字段的可选值，从Types模型的对象中获取seconds字段的值作为候选项
            db_field.choices = [(x['seconds'], x['seconds']) for x in Types.objects.values('seconds') ]
        # 调用父类的方法，返回处理后的字段表单配置
        return super().formfield_for_dbfield(db_field, **kwargs)
    fieldsets = (
        # 第一组
        ('商品信息',{
            # 显示的字段
            'fields': ('name', 'sezes', 'types', 'price', 'discount')
        }),
        # 第二组
        ('收藏数量',{
            # collapse，让这个分组默认是折叠的（可展开）
            'classes': ('collapse',),
            'fields': ('likes',),
        }),
    )
    ordering = ['id']
    # 支持哪些字段点击表头进行排序
    sortable_by = ('price', 'discount')
    # 定义在数据列表页显示的模型字段
    list_display = ['id', 'name','sezes','types', 'price', 'discount']
    # 在数据列表的右侧添加筛选栏，按照types字段进行筛选
    list_filter = ['types']
    # 设置分页每页显示的数据条数
    list_per_page = 100
    # 最大显示数量
    list_max_show_all = 200
    # 允许在数据列表页面直接编辑name字段的值，不需要进入修改页面
    list_editable = ['name']
    # 在列表页面添加搜素框，支持按照name和types字段的值进行搜素数据
    search_fields = ['name', 'types']
    # 启用另存为按钮
    save_as = True
    # 下拉框位置，False表示不显示在顶部
    actions_on_top = False
    actions_on_bottom = True
    def get_readonly_fields(self, request, obj = None):
        if request.user.is_superuser:
            self.readonly_fields = []
        else:
            self.readonly_fields = ['types']
        return self.readonly_fields

# 根据当前的用户名设置数据访问权限
def get_queryset(self, request):
    qs = super().get_queryset(request)
    if request.user.is_superuser:
        return qs
    else:
        return qs.filter(id__lt=2)

def save_model(self, request, obj, form, change):
    if change:
        user = request.user.username
        name = self.model.objects.get(pk=obj.pk).name
        types = form.cleaned_data['types']
        f = open("d://log.txt", 'a', encoding='utf-8')
        f.write(name + "商品类型：" + types + "被" + user + "修改" + '\r\n')
        f.close()
    else:
        pass
    super().save_model(request, obj, form, change)

def get_datas(self, request, queryset):
    """
        queryset:代表已经被勾选的数据对象
        """
    temp = []
    for d in queryset:
        t = [d.name, d.types, str(d.discount)]
        temp.append(t)
        f = open("d://data.txt", 'a', encoding='utf-8')
    for t in temp:
        f.write(",".join(t) + '\r\n')
        f.close()

    self.message_user(request, "数据导出成功")
    # 设置函数的显示名称
    get_datas.short_description = "导出所选数据"
# 添加到acton中
actions = ['get_datas']